import dialogHtml from 'ngtemplate!html!../../../tpls/dialog/success.dialog.html';
import dialogUnloadHtml from 'ngtemplate!html!../../../tpls/dialog/success.unload.dialog.html';
/**
 * dialogService - factory
 * 封装一个http服务实现模态框快速调用
 */
class dialogService {
    constructor($rootScope, $state, $timeout, ngDialog) {

        this.$rootScope = $rootScope;
        this.$state     = $state;
        this.$timeout   = $timeout;
        this.ngDialog   = ngDialog;

        this.success = (text, params) => {

            // 模态框显示文本
            $rootScope.text = text || '成功调用';

            // 提示用户成功调用
            this.ngDialog.open({
                template: dialogHtml,
                className: 'ngdialog-theme-default ngdialog-alert ngdialog-success',
                scope: $rootScope
            });

            // 延迟1.5秒后, 关闭模态框并跳转路由
            this.$timeout( () => {
                this.ngDialog.close();
                params ? this.$state.go(params) : this.$state.reload(); 
            }, 1500)
        }

        this.successUnload = (text, params) => {

            // 模态框显示文本
            $rootScope.text = text || '成功调用';

            // 提示用户成功调用
            this.ngDialog.open({
                template: dialogUnloadHtml,
                className: 'ngdialog-theme-default ngdialog-alert ngdialog-success',
                scope: $rootScope
            });

            // 延迟1.5秒后, 关闭模态框并跳转路由
            this.$timeout( () => {
                this.ngDialog.close();
            }, 1500)
        }


        this.error = (text, params) => {

            var text   = text || '验证失败',
                params = params || false;

            // 提示用户操作失败
            this.ngDialog.open({
                template: '<p>' + text + '</p>',
                className: 'ngdialog-theme-default ngdialog-alert',
                plain: true
            });

            // 延迟1.5秒后, 关闭模态框并跳转路由
            if (!params) return;
            if (params){
                this.$timeout( () => {
                    this.ngDialog.close();
                    this.$state.go('loginindex.login'); 
                }, 1500)
            }
        }
    }

    success(text, params) {
        return this.success(text, params)
    }

    error(text, params) {
        return this.error(text, params)
    }

    successUnload(text, params) {
        return this.successUnload(text, params)
    }

}

dialogService.$inject = ['$rootScope', '$state', '$timeout', 'ngDialog'];

export default dialogService;